package ui;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sxpjyy.monkey.mobilenurse.R;

import java.util.ArrayList;
import java.util.List;

import adapters.JYJGListAdapter;
import adapters.JYXMListAdapter;
import mytools.MyBaseActivity;
import objects.InPatient;
import objects.LisJYJG;
import objects.LisJYXM;
import service.WebService;

public class ReportJYActivity extends MyBaseActivity implements AdapterView.OnItemClickListener {

    private ListView jyxmListView; //检验项目

    private JYXMListAdapter jyxmAdapter;
    private ListView jyjgListView;//检查结果

    private JYJGListAdapter jyjgAdapter;

    private String ZYHM = null;
    private String chooseBGDH = null;

    private String mJyxmResult = null;
    private String mJyjgResult = null;
    private List<LisJYXM> mJyxmList;
    private List<LisJYJG> mJyjgList;

    private boolean isJyxmLoaded = false;
    private boolean isJyjgLoaded = false;

    private static ProgressDialog progressDialog = null;

    public ReportJYActivity() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_report_jy);
        onBindUI();

        jyxmListView.setOnItemClickListener(this);
        jyjgListView.setOnItemClickListener(this);

        jyxmAdapter = new JYXMListAdapter(this);
        jyxmListView.setAdapter(jyxmAdapter);

        jyjgAdapter = new JYJGListAdapter(this);
        jyjgListView.setAdapter(jyjgAdapter);
    }

    private void onBindUI() {
        jyxmListView = findViewById(R.id.jy_item_list);
        jyjgListView = findViewById(R.id.jy_result_list);
    }

    @Override
    protected void onResume() {
        super.onResume();
        InPatient inPatient = getMyApplication().getInPatient();
        if (inPatient != null && inPatient.getZYHM() != null) {
            ZYHM = inPatient.getZYHM();
            if (ZYHM != null && !ZYHM.equals("")) {
                showProgressBar(this);
                new MyJyxmTask().execute();
                new MyJyjgTask().execute();
//                isGettingJYXM = true;
            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        switch (adapterView.getId()) {
            case R.id.jy_item_list:
                if (mJyxmList.get(position) != null) {
                    chooseBGDH = mJyxmList.get(position).getBGDH();
                    setJYJG();
                }
                break;
            case R.id.jy_result_list:
                break;
            default:
                break;
        }
    }



    //显示进度对话框
    private static void showProgressBar(Context context) {
        progressDialog = new ProgressDialog(context);
        progressDialog.setMessage("正在获取信息。。。");
        progressDialog.setCancelable(false);
        progressDialog.show();
    }

    //取消进度对话框
    private static void cancelProgressBar() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.cancel();
            progressDialog = null;
        }
    }

    private class MyJyxmTask extends AsyncTask<Void, Void, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(Void... voids) {
            WebService jyxmWebService = new WebService(ReportJYActivity.this);
            jyxmWebService.setMETHOD_NAME("getJyxmList");
            if (ZYHM.equals("")) {
                return "住院号码为空!";
            } else {
                String result;
                String[] Parms = {ZYHM};
                jyxmWebService.setPARMS(Parms);
                result = jyxmWebService.getServerData();
                if (result == null || result.equals("null") || result.equals("") || result.equals("[]")) {
                    return "获取检验项目列表失败！";
                } else {
                    if (!result.equals(mJyxmResult)) {
                        mJyxmResult = result;
                        Gson gson = new Gson();
                        mJyxmList = gson.fromJson(mJyxmResult, new TypeToken<List<LisJYXM>>() {
                        }.getType());
                        if (mJyxmList != null && mJyxmList.size() > 0) {
                            return null;
                        } else {
                            return "获取检验项目列表为空！";
                        }
                    } else {
                        return "数据未发生变化!";
                    }
                }
            }
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (s != null) {
                Toast.makeText(ReportJYActivity.this, s, Toast.LENGTH_SHORT).show();
            } else {
                doFirstLoadList();
            }
        }
    }


    private class MyJyjgTask extends AsyncTask<Void, Void, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(Void... voids) {
            WebService jyjgWebService = new WebService(ReportJYActivity.this);
            jyjgWebService.setMETHOD_NAME("getJyjgList");

            if (ZYHM.equals("")) {
                return "住院号码为空!";
            } else {
                String result;
                String[] Parms = {ZYHM};
                jyjgWebService.setPARMS(Parms);
                result = jyjgWebService.getServerData();
                if (result == null || result.equals("null") || result.equals("") || result.equals("[]")) {
                    return "获取检验结果失败!";
                } else {
                    if (!result.equals(mJyjgResult)) {
                        mJyjgResult = result;
                        Gson gson = new Gson();
                        mJyjgList = gson.fromJson(mJyjgResult, new TypeToken<List<LisJYJG>>() {
                        }.getType());
                        if (mJyjgList != null && mJyjgList.size() > 0) {
                            return null;
                        } else {
                            return "获取检验结果为空!";
                        }
                    } else {
                        return "检验结果数据未发生变化!";
                    }
                }
            }
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            cancelProgressBar();
            if (s != null) {
                Toast.makeText(ReportJYActivity.this, s, Toast.LENGTH_SHORT).show();
            } else {
                doFirstLoadList();
            }
        }
    }


    /**
     * 过滤检验结果
     */
    private List<LisJYJG> getFilterList() {
        if (mJyjgList != null && mJyjgList.size() > 0 && chooseBGDH != null) {
            List<LisJYJG> list = new ArrayList<>();
            for (LisJYJG lisJYJG : mJyjgList
                    ) {
                if (lisJYJG.getBGDH().equals(chooseBGDH)) {
                    list.add(lisJYJG);
                }
            }
            if (list.size() > 0) {
                return list;
            }
        }
        return null;
    }

    private void setJYJG() {
        List<LisJYJG> filterList = getFilterList();
        if (filterList != null && filterList.size() > 0) {
            jyjgAdapter.setList(filterList);
            jyjgAdapter.notifyDataSetChanged();
            isJyjgLoaded = true;
            cancelProgressBar();
        }
    }

    private void doFirstLoadList() {
        if (!isJyxmLoaded && mJyxmList != null && mJyxmList.size() > 0) {
            jyxmAdapter.setList(mJyxmList);
            jyxmAdapter.notifyDataSetChanged();
            chooseBGDH = mJyxmList.get(0).getBGDH();
            isJyxmLoaded = true;
        }
        if (!isJyjgLoaded) {
            setJYJG();
        }
    }
}
